#pragma once

#include "wx/grid.h"
#include "prs_Data.h"

namespace app {
///////////////////////////////////////////////////////////////////////////////
struct TGridCtrl;
///////////////////////////////////////////////////////////////////////////////

struct TGridTable : public wxGridTableBase,	public svl::TDebug
{
	TGridCtrl	&GridCtrl;
	//
	TGridTable(TGridCtrl &GridCtrl);
	
  void ConnectData(prs::TDataTable* DataTable);
  void ConnectData(prs::TDataTableRecord* DataTableRecord);
	
	int GetNumberRows();
	int GetNumberCols();
    
	wxString GetColLabelValue( int col );
	wxString GetRowLabelValue( int row );
	wxString GetTypeName(int row, int col);

	wxString GetValue( int row, int col );
	long GetValueAsLong( int row, int col );
	double GetValueAsDouble( int row, int col );

  void SetValue( int row, int col, const wxString& str );
	void SetValueAsLong( int row, int col, long value );
	void SetValueAsDouble( int row, int col, double value );

	virtual bool CanGetValueAs( int row, int col, const wxString& typeName );
  virtual bool CanSetValueAs( int row, int col, const wxString& typeName );

  virtual bool IsEmptyCell( int row, int col );

private:
	prs::TDataTable* GetDataTable() {
		if (dataTable) {
			return dataTable;
		}
		else if (dataTableRecord)	{
			return dataTableRecord->GetTable();
		}
		return NULL;
	}
	prs::TDataTableRecord *GetDataTableRecord(int Row) {
		if (dataTable) {
			return &dataTable->Records.Array.Get(Row);
		}
		else if (dataTableRecord)	{
			return dataTableRecord;
		}
		return NULL;
	}

	prs::TDataTable* dataTable;
	prs::TDataTableRecord* dataTableRecord;
	svl_Except;
};

///////////////////////////////////////////////////////////////////////////////

struct TGridCtrl : public wxGrid
{
	prs::TDataTables &DataTables;
	//
	TGridCtrl(wxWindow *parent
		,	prs::TDataTables &DataTables
		, wxWindowID id
		, const wxPoint& pos = wxDefaultPosition
		, const wxSize& size = wxDefaultSize
		, long style = 0
		, const wxString& name = wxGridNameStr
  );
 ~TGridCtrl();
	//
	void ConnectData(prs::TDataTable* DataTable);
  void ConnectData(prs::TDataTableRecord* DataTableRecord);
	void DisconnectData();

private:	
	TGridTable gridTable;
};

///////////////////////////////////////////////////////////////////////////////
}
